package com.kensure.shike.tj.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.frame.JSBaseService;
import co.kensure.mem.DateUtils;
import co.kensure.mem.MapUtils;

import com.kensure.shike.tj.dao.SKZhangStatisticsDao;
import com.kensure.shike.tj.model.YMDZhiBiao;
import com.kensure.shike.user.service.SKUserService;

/**
 * 用户表统计方法 服务实现类
 * 
 * @author fankd created on 2019-03-11
 * @since
 */
@Service
public class StatisticsService extends JSBaseService {

	@Resource
	private SKZhangStatisticsDao zhangdao;

	@Resource
	private SKUserService sKUserService;

	// 成员
	private YMDZhiBiao selectUser() {
		Date now = new Date();
		// 本日的开始和结束
		Date dayStart = DateUtils.parse(DateUtils.formatDateStart(now), DateUtils.DATE_FORMAT_PATTERN);
		Date dayEnd = DateUtils.parse(DateUtils.formatDateEnd(now), DateUtils.DATE_FORMAT_PATTERN);

		// 本月的开始和结束
		Date monthStart = DateUtils.parse(DateUtils.format(now, "yyyy-MM-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date monthEnd = DateUtils.getPastMonth(monthStart, 1);

		// 本月的开始和结束
		Date yearStart = DateUtils.parse(DateUtils.format(now, "yyyy-01-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date yearEnd = DateUtils.getPastMonth(monthStart, 12);

		Map<String, Object> parameters = MapUtils.genMap("startCreatedTime", dayStart, "endCreatedTime", dayEnd);
		Long benri = sKUserService.selectCountByWhere(parameters);

		parameters = MapUtils.genMap("startCreatedTime", monthStart, "endCreatedTime", monthEnd);
		Long benyue = sKUserService.selectCountByWhere(parameters);

		parameters = MapUtils.genMap("startCreatedTime", yearStart, "endCreatedTime", yearEnd);
		Long bennian = sKUserService.selectCountByWhere(parameters);

		YMDZhiBiao zb = new YMDZhiBiao();
		zb.setName("成员情况");
		zb.setBenri(benri.doubleValue());
		zb.setBenyue(benyue.doubleValue());
		zb.setBennian(bennian.doubleValue());
		return zb;
	}

	// 会员
	private YMDZhiBiao selectVip() {
		Date now = new Date();
		// 本日的开始和结束
		Date dayStart = DateUtils.parse(DateUtils.formatDateStart(now), DateUtils.DATE_FORMAT_PATTERN);
		Date dayEnd = DateUtils.parse(DateUtils.formatDateEnd(now), DateUtils.DATE_FORMAT_PATTERN);

		// 本月的开始和结束
		Date monthStart = DateUtils.parse(DateUtils.format(now, "yyyy-MM-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date monthEnd = DateUtils.getPastMonth(monthStart, 1);

		// 本月的开始和结束
		Date yearStart = DateUtils.parse(DateUtils.format(now, "yyyy-01-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date yearEnd = DateUtils.getPastMonth(monthStart, 12);

		Map<String, Object> parameters = MapUtils.genMap("startCreatedTime", dayStart, "endCreatedTime", dayEnd, "startLevelCode", "0001");
		Long benri = sKUserService.selectCountByWhere(parameters);

		parameters = MapUtils.genMap("startCreatedTime", monthStart, "endCreatedTime", monthEnd, "startLevelCode", "0001");
		Long benyue = sKUserService.selectCountByWhere(parameters);

		parameters = MapUtils.genMap("startCreatedTime", yearStart, "endCreatedTime", yearEnd, "startLevelCode", "0001");
		Long bennian = sKUserService.selectCountByWhere(parameters);

		YMDZhiBiao zb = new YMDZhiBiao();
		zb.setName("会员情况");
		zb.setBenri(benri.doubleValue());
		zb.setBenyue(benyue.doubleValue());
		zb.setBennian(bennian.doubleValue());
		return zb;
	}

	// 销售
	private YMDZhiBiao selectSale() {
		Date now = new Date();
		// 本日的开始和结束
		Date dayStart = DateUtils.parse(DateUtils.formatDateStart(now), DateUtils.DATE_FORMAT_PATTERN);
		Date dayEnd = DateUtils.parse(DateUtils.formatDateEnd(now), DateUtils.DATE_FORMAT_PATTERN);
		// 本月的开始和结束
		Date monthStart = DateUtils.parse(DateUtils.format(now, "yyyy-MM-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date monthEnd = DateUtils.getPastMonth(monthStart, 1);
		// 本月的开始和结束
		Date yearStart = DateUtils.parse(DateUtils.format(now, "yyyy-01-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date yearEnd = DateUtils.getPastMonth(monthStart, 12);

		Map<String, Object> parameters = MapUtils.genMap("startCreatedTime", dayStart, "endCreatedTime", dayEnd, "busitypeid", 1, "status", 1);
		Double benri = zhangdao.sumZhangYue(parameters);

		parameters = MapUtils.genMap("startCreatedTime", monthStart, "endCreatedTime", monthEnd, "busitypeid", 1, "status", 1);
		Double benyue = zhangdao.sumZhangYue(parameters);

		parameters = MapUtils.genMap("startCreatedTime", yearStart, "endCreatedTime", yearEnd, "busitypeid", 1, "status", 1);
		Double bennian = zhangdao.sumZhangYue(parameters);

		YMDZhiBiao zb = new YMDZhiBiao();
		zb.setName("销售情况");
		zb.setBenri(benri);
		zb.setBenyue(benyue);
		zb.setBennian(bennian);
		return zb;
	}

	// 充值
	private YMDZhiBiao selectChongzhi() {
		Date now = new Date();
		// 本日的开始和结束
		Date dayStart = DateUtils.parse(DateUtils.formatDateStart(now), DateUtils.DATE_FORMAT_PATTERN);
		Date dayEnd = DateUtils.parse(DateUtils.formatDateEnd(now), DateUtils.DATE_FORMAT_PATTERN);
		// 本月的开始和结束
		Date monthStart = DateUtils.parse(DateUtils.format(now, "yyyy-MM-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date monthEnd = DateUtils.getPastMonth(monthStart, 1);
		// 本月的开始和结束
		Date yearStart = DateUtils.parse(DateUtils.format(now, "yyyy-01-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date yearEnd = DateUtils.getPastMonth(monthStart, 12);

		Map<String, Object> parameters = MapUtils.genMap("startCreatedTime", dayStart, "endCreatedTime", dayEnd, "busitypeid", 101, "status", 1);
		Double benri = zhangdao.sumZhangYue(parameters);

		parameters = MapUtils.genMap("startCreatedTime", monthStart, "endCreatedTime", monthEnd, "busitypeid", 101, "status", 1);
		Double benyue = zhangdao.sumZhangYue(parameters);

		parameters = MapUtils.genMap("startCreatedTime", yearStart, "endCreatedTime", yearEnd, "busitypeid", 101, "status", 1);
		Double bennian = zhangdao.sumZhangYue(parameters);

		YMDZhiBiao zb = new YMDZhiBiao();
		zb.setName("充值情况");
		zb.setBenri(benri);
		zb.setBenyue(benyue);
		zb.setBennian(bennian);
		return zb;
	}

	// 会员缴费
	private YMDZhiBiao selectJiaofei() {
		Date now = new Date();
		// 本日的开始和结束
		Date dayStart = DateUtils.parse(DateUtils.formatDateStart(now), DateUtils.DATE_FORMAT_PATTERN);
		Date dayEnd = DateUtils.parse(DateUtils.formatDateEnd(now), DateUtils.DATE_FORMAT_PATTERN);
		// 本月的开始和结束
		Date monthStart = DateUtils.parse(DateUtils.format(now, "yyyy-MM-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date monthEnd = DateUtils.getPastMonth(monthStart, 1);
		// 本月的开始和结束
		Date yearStart = DateUtils.parse(DateUtils.format(now, "yyyy-01-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date yearEnd = DateUtils.getPastMonth(monthStart, 12);

		Map<String, Object> parameters = MapUtils.genMap("startCreatedTime", dayStart, "endCreatedTime", dayEnd, "busitypeid", 3, "status", 1);
		Double benri = zhangdao.sumZhangYue(parameters);

		parameters = MapUtils.genMap("startCreatedTime", monthStart, "endCreatedTime", monthEnd, "busitypeid", 3, "status", 1);
		Double benyue = zhangdao.sumZhangYue(parameters);

		parameters = MapUtils.genMap("startCreatedTime", yearStart, "endCreatedTime", yearEnd, "busitypeid", 3, "status", 1);
		Double bennian = zhangdao.sumZhangYue(parameters);

		YMDZhiBiao zb = new YMDZhiBiao();
		zb.setName("会员缴费");
		zb.setBenri(benri);
		zb.setBenyue(benyue);
		zb.setBennian(bennian);
		return zb;
	}

	// 提现情况
	private YMDZhiBiao selectTiXian() {
		Date now = new Date();
		// 本日的开始和结束
		Date dayStart = DateUtils.parse(DateUtils.formatDateStart(now), DateUtils.DATE_FORMAT_PATTERN);
		Date dayEnd = DateUtils.parse(DateUtils.formatDateEnd(now), DateUtils.DATE_FORMAT_PATTERN);
		// 本月的开始和结束
		Date monthStart = DateUtils.parse(DateUtils.format(now, "yyyy-MM-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date monthEnd = DateUtils.getPastMonth(monthStart, 1);
		// 本月的开始和结束
		Date yearStart = DateUtils.parse(DateUtils.format(now, "yyyy-01-01 00:00:00"), DateUtils.DATE_FORMAT_PATTERN);
		Date yearEnd = DateUtils.getPastMonth(monthStart, 12);

		Map<String, Object> parameters = MapUtils.genMap("startCreatedTime", dayStart, "endCreatedTime", dayEnd, "busitypeid", 2, "status", 1);
		Double benri = zhangdao.sumZhangYue(parameters);

		parameters = MapUtils.genMap("startCreatedTime", monthStart, "endCreatedTime", monthEnd, "busitypeid", 2, "status", 1);
		Double benyue = zhangdao.sumZhangYue(parameters);

		parameters = MapUtils.genMap("startCreatedTime", yearStart, "endCreatedTime", yearEnd, "busitypeid", 2, "status", 1);
		Double bennian = zhangdao.sumZhangYue(parameters);

		YMDZhiBiao zb = new YMDZhiBiao();
		zb.setName("提现情况");
		zb.setBenri(benri);
		zb.setBenyue(benyue);
		zb.setBennian(bennian);
		return zb;
	}

	// 余额
	private YMDZhiBiao selectYue() {
		Double bennian = zhangdao.sumYue(null);
		YMDZhiBiao zb = new YMDZhiBiao();
		zb.setName("用户余额");
		zb.setBenri(bennian);
		zb.setBenyue(bennian);
		zb.setBennian(bennian);
		return zb;
	}

	/**
	 * 首页
	 */
	public List<YMDZhiBiao> selectIndex() {

		List<YMDZhiBiao> list = new ArrayList<>();

		YMDZhiBiao user = selectUser();
		list.add(user);
		YMDZhiBiao vip = selectVip();
		list.add(vip);
		YMDZhiBiao sale = selectSale();
		list.add(sale);
		YMDZhiBiao chongzhi = selectChongzhi();
		list.add(chongzhi);
		YMDZhiBiao jiaofei = selectJiaofei();
		list.add(jiaofei);
		YMDZhiBiao tixian = selectTiXian();
		list.add(tixian);
		YMDZhiBiao yue = selectYue();
		list.add(yue);
		return list;
	}

}
